這題主要使用Hashmap,將相同size的人放在同一格hash裡,再從裡面算組數丟進答案中
class Solution {
public:
vector<vector<int>> groupThePeople(vector<int>& groupSizes) {
unordered_map<int, vector<int>> umap;
for(int i = 0; i < groupSizes.size(); i++){
umap[groupSizes[i]].push_back(i);
}
vector<vector<int>> ans;
for(auto [x, v] : umap){
int q = umap[x].size() / x;
for(int i = 0; i < q; i++){
vector<int> tmp;
for(int j = 0; j < x; j++){
tmp.push_back(umap[x][j + i * x]);
}
ans.push_back(tmp);
}
}
return ans;
}
};